package com.dc.datacollect.utils;

import org.springframework.beans.BeanUtils;

/**
 * 实体dto转换器
 *
 * @author zhuangjie
 * @date 2023/04/26
 */
public class EntityDtoConverter {


    /**
     * dto转实体
     *
     * @param dto         dto
     * @param entityClass 实体类
     * @return {@link T}
     */
    public static <T> T dtoToEntity(Object dto, Class<T> entityClass) {
        T entity = null;
        try {
            entity = entityClass.newInstance();
            BeanUtils.copyProperties(dto, entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }

    /**
     * 多个实体转dto
     * @param entities 实体
     * @param dtoClass dto类
     * @return {@link T}
     */
    public static <T> T entitiesToDto(Object[] entities, Class<T> dtoClass) {
        T t = null;
        try {
            t = dtoClass.newInstance();
            for (Object entity : entities) {
                BeanUtils.copyProperties(entity,t);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return t;
    }


}


